package com.pactera.madp.cp.controller.oms.marketing;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.marketing.MarketingRecUpdateDTO;
import com.pactera.madp.cp.api.page.MarketingPage;
import com.pactera.madp.cp.api.vo.marketing.MarketingRecVO;
import com.pactera.madp.cp.common.util.PageUtil;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.MarketingRecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * @Auther: yuShen.qi
 * @Date: 2021/5/25:16:06
 * @Description:
 */
@RestController
@RequestMapping("/marketing-rec")
@Api(value = "marketingRec", tags = "营销素材-分类管理-推荐")
public class MarketingRecController {

    /**
     * 服务对象
     */
    @Resource
    private MarketingRecService marketingRecService;

    /**
     * 通过标题模糊查询素材
     *
     * @param title
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "通过标题模糊查询素材")
    public R<IPage> getListByTitle(String title, Page page){
       return R.ok(marketingRecService.allList(title, page));
    }

    /**
     * 通过标题选取对应类型数据
     *
     * @param title
     * @param type
     * @return
     */
    @GetMapping("/choose")
    @ApiOperation(value = "通过标题选取对应类型数据")
    public R<Boolean> changeList(String title, Integer type,Integer classification){
       Integer corpId = CorpContextHolder.getCorpId();
       return R.ok(marketingRecService.changeList(title, type,corpId,classification));
    }

    /**
     * 通过标题模糊查询对应类型数据
     *
     * @param page
     * @param title
     * @param type
     * @return
     */
    @GetMapping("/page-type")
    @ApiOperation(value = "通过标题模糊查询对应类型数据")
    public R<IPage> listByType(Page page,String title,Integer type){
       return R.ok( marketingRecService.listByType(page,title, type));
    }

    /**
     * 查询已添加在推荐榜单中的数据
     *
     * @return
     */
   @GetMapping("/index")
   @ApiOperation(value = "查询已添加在推荐榜单中的数据.可模糊查询")
   public R<Page<MarketingRecVO>> list(MarketingPage page, String title){
       List<MarketingRecVO> marketingRecVOS = marketingRecService.allListByPage(title);
       return R.ok(PageUtil.split(marketingRecVOS,page.getPageCurrent(),page.getPageSize()));
   }
   /**
     * 查询已添加在推荐榜单中的数据
     *
     * @return
     */
   @GetMapping("/index-type")
   @ApiOperation(value = "根据类型查询已添加在推荐榜单中的数据，可模糊查询")
   public R<Page<MarketingRecVO>> listByPageAndType(MarketingPage page,String title,Integer type){
       List<MarketingRecVO> marketingRecVOS = marketingRecService.allListByPageAndType(title, type);
       //根据sort排序
       marketingRecVOS.sort(Comparator.comparing(v->v.getSort()));
       return R.ok(PageUtil.split(marketingRecVOS,page.getPageCurrent(),page.getPageSize()));
   }

    /**
     * 逻辑删除对应数据
     *
     * @param id
     * @param type
     * @param title
     * @return
     */
    @SysLog("营销素材-分类管理-推荐-取消推荐")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "取消推荐")
    public R<Boolean> delete(@PathVariable Integer id,Integer type,String title,Integer classification){
     return R.ok(marketingRecService.deleteById(id,type,title,classification));
   }

    /**
     * 推荐数据自定义排序
     *
     * @param dto
     * @return
     */
   @PostMapping
   @ApiOperation(value = "推荐数据自定义排序")
    public R changeRanking(@RequestBody MarketingRecUpdateDTO dto){
       marketingRecService.changeRanking(dto);
     return R.ok();
   }

}
